<template>
  <div class="tab-bar-item" @click="ItemClick">
    <!-- 使用具名插槽 -->
    <div v-if="!isActive"><slot name="item-icon"></slot></div>
    <div v-else><slot name="item-icon-active"></slot></div>
    <div :style="activeStyle">
      <slot name="item-text"></slot>
    </div>
  </div>
</template>

<script>
export default {
  name: "TabBarItem",
  props: {
    // 用于接收点击的路径
    path: {
      type: String,
      default: "/home",
    },
    // 用于接收用户传入的点击颜色
    activeColor: {
      type: String,
      default: "red",
    },
  },
  data() {
    return {};
  },
  computed: {
    isActive() {
      // 判断当前活跃路由的路径是否等于点击的路径
      return this.$route.path == this.path;
    },
    activeStyle() {
      // 点击了当前路由则把颜色改为用户指定的颜色，否则为黑色
      return this.isActive ? { color: this.activeColor } : {};
    },
  },
  methods: {
    ItemClick() {
      // 实现路由跳转
      this.$router.push(this.path).catch((err) => {
        console.log("输出报错", err);
      });
    },
  },
};
</script>

<style>
.tab-bar-item {
  flex: 1;
  text-align: center;
  height: 49px;
  width: 25vw;
  font-size: 14px;
}
.tab-bar-item img {
  height: 20px;
  width: 20px;
  margin-top: 3px;
  margin-bottom: 2px;
  /* 去除图片下面的空间 */
  vertical-align: middle;
}
</style>